<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">||||</div>
        <!-- 侧边栏菜单区域-->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
        <!-- router:开启路由模式， 开启之后，激活导航时会以index作为path进行路由跳转-->
        <!-- collapse：水平折叠收起菜单 collapse-transition：收起菜单的动画默认开启 -->
        <!-- unique-opened:每一次展示一个 -->
          <!--  一级菜单 -->
          <el-submenu
            :index="''+item.id"
            v-for="item in MenuList"
            :key="item.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.AuthName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+item1.path"
              v-for="item1 in item.children"
              :key="item1.id"
              @click="saveNacState('/'+item1.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ item1.AuthName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      MenuList: [],
      iconList: {
        1: 'el-icon-service',
        2: 'el-icon-star-off',
        3: 'el-icon-goods',
        4: 'el-icon-document',
        5: 'el-icon-date'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //  获取菜单栏数据
    async getMenuList () {
      const { data: res } = await this.$http.get('getmenu')
      if (res.meta.status !== 200) return this.$message.error('获取菜单失败')
      this.MenuList = res.data
      console.log(res.data)
    },
    //  切换菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    //  点击二级菜单的事件
    saveNacState (activePath) {
      console.log(activePath)
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  img {
    width: 60px;
    height: 60px;
    border-radius: 100%;
  }
  div {
    display: flex;
    align-items: center;
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: none;
    // 清除展示出来的边框线
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color:#fff;
  text-align: center;
  // 文字间距
  letter-spacing: 0.2em;
  // 鼠标经过变成小手
  cursor:pointer
}
</style>
